<?php
	include_once 'isLogin.php';
	$user_table='user';					//会员

	//验证账号唯一性
	if($_GET['action']=='checkAccount'){
		$account=$_POST['account'];			//账号
		$sql=$user_table.' where account="'.$account.'"';
		$list=$commonc->getlist($sql,'','id');
		$error=0;
		$result=array('error'=>$error,'msg'=>$msg);
		echo json_encode($result);
		exit();
	}

	//会员信息
	$userc=new user();
	$user_id=$_GET['id'];					//会员id
	$key='*';
	$user=$userc->userInfo($user_id);
	$smarty->assign('user',$user);

	//会员信息提交
	if($_POST['submit']){
		if($_GET['id']!=''){
			$avatar=$user['avatar'];
		}
		//上传图片，生成缩略图
		if($_FILES['avatar']['name']!=''){
			//设置路径
			$folder = "../upload/user/";
			$annexFolder = $folder."large/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$avatar);
			//@unlink($middleFolder.$img);
			@unlink($smallFolder.$avatar);
			//上传新图
			$upimg = new uploadImg();
			$avatar = $upimg->upLoad('avatar',$annexFolder);
			//小图
			$upimg->smallImg($annexFolder.$avatar,$smallFolder);
		}

		$pwd=md5($_POST['pwd']);
		$arr= array('account' =>$_POST['account'],'phone'=>$_POST['phone'],
					'real_name' =>$_POST['real_name'],'email'=>$_POST['email'],
					'avatar' =>$avatar,
					'sex' =>$_POST['sex'],
					'self_intro' =>$_POST['self_intro'],
					'address'=>$_POST['address']
			 );
		if($_GET['id']!=''){
			$res=$commonc->update($user_table,$arr,'id="'.$_GET['id'].'"');
			$msg="更新成功！";
			$href="user_add.php?id=".$_GET['id'];
		}else{
			$arr['reg_time']=time();
			$arr['pwd']=$pwd;
			$res=$commonc->insert($user_table,$arr);
			$msg="添加成功！";
			$href="admin_add.php";
		}
		echo $commonc->submitAlert($msg,$href);
		exit;
	}
	
	$smarty->display("user_add.html");
?>